package geektime.spring.springbucks.waiterservice.integration;

import geektime.spring.springbucks.waiterservice.model.CoffeeOrder;
import geektime.spring.springbucks.waiterservice.service.CoffeeOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class orderListener {
  @Autowired
  private Customer customer;

  @Autowired
  private CoffeeOrderService orderService;

  @StreamListener(Barista.FINISHED_ORDERS)
  public void  listenFinishedOrders(Long id){
    log.info("We've finished an order [{}].",id);

    CoffeeOrder coffeeOrder = null;
    try {
      coffeeOrder = orderService.get(id);
    } catch (Exception e) {
      e.printStackTrace();
    }
    log.info("测试: {}",coffeeOrder);
    Message<Long> message = MessageBuilder.withPayload(id)
        .setHeader("customer", coffeeOrder.getCustomer())
        .build();
    log.info("Notify the customer: {}",coffeeOrder.getCustomer());
    customer.notification().send(message);
  }

}
